package com.maxd.controller.helpTask;

import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.HelpClassify;
import com.maxd.model.HelpClassifyDetails;
import com.maxd.service.helpTask.HelpClassifyDetailsService;
import com.maxd.service.helpTask.IHelpClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

/**
 * @author fang
 * @date 2020/6/11
 */
@RestController
@Api(value = "助力分类", tags = {"助力分类"})
@RequestMapping(value = "/helpClassify")
public class HelpClassifyController {

    @Autowired
    private IHelpClassifyService helpClassifyService;
    @Autowired
    private HelpClassifyDetailsService helpClassifyDetailsService;


    @RequestMapping(value = "/saveHelpClassify", method = RequestMethod.POST)
    @ApiOperation("新建分类")
    @ResponseBody
    public Result saveHelpClassify(@RequestBody HelpClassify helpClassify) {
        if(helpClassify.getClassifyType()==null){
            helpClassify.setClassifyType(1);
        }
        helpClassifyService.save(helpClassify);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/updateHelpClassify", method = RequestMethod.POST)
    @ApiOperation("修改分类")
    @ResponseBody
    public Result updateHelpClassify(@RequestBody HelpClassify helpClassify) {
        if(helpClassify.getClassifyType()==null){
            helpClassify.setClassifyType(1);
        }
        helpClassifyService.update(helpClassify);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteClassifyById", method = RequestMethod.POST)
    @ApiOperation("删除分类")
    @ResponseBody
    public Result deleteClassifyById(Long id) {
        helpClassifyService.deleteById(id);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteClassifyByIds", method = RequestMethod.POST)
    @ApiOperation("删除多个分类")
    @ResponseBody
    public Result deleteClassifyByIds(String ids) {
        helpClassifyService.deleteByIdList(ids);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectClassifyById", method = RequestMethod.GET)
    @ApiOperation("根据id查看分类详细信息")
    @IgnoreSecurity
    @ResponseBody
    public Result selectClassifyById(Long id) {
        return ResultUtil.success(helpClassifyService.selectById(id));
    }


    @RequestMapping(value = "/selectClassifyList", method = RequestMethod.GET)
    @ApiOperation("查看分类")
    @IgnoreSecurity
    @ResponseBody
    public Result selectClassifyList(@RequestParam(required = false) Integer classifyType) {
        if(classifyType==null){
            classifyType=1;
        }
        return ResultUtil.success(helpClassifyService.selectList(classifyType));
    }


    @RequestMapping(value = "/saveHelpClassifyDetails", method = RequestMethod.POST)
    @ApiOperation("新建二级分类")
    @ResponseBody
    public Result saveHelpClassifyDetails(@RequestBody HelpClassifyDetails helpClassifyDetails) {
        helpClassifyDetailsService.save(helpClassifyDetails);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/updateClassifyDetails", method = RequestMethod.POST)
    @ApiOperation("修改二级分类")
    @ResponseBody
    public Result updateClassifyDetails(@RequestBody HelpClassifyDetails helpClassifyDetails) {
        helpClassifyDetailsService.update(helpClassifyDetails);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/deleteClassifyDetailsById", method = RequestMethod.POST)
    @ApiOperation("删除二级分类")
    @ResponseBody
    public Result deleteClassifyDetailsById(Long id) {
        helpClassifyDetailsService.deleteById(id);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/deleteClassifyDetailsByIds", method = RequestMethod.POST)
    @ApiOperation("删除多个二级分类")
    @ResponseBody
    public Result deleteClassifyDetailsByIds(String ids) {
        helpClassifyDetailsService.deleteByIdList(ids);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectClassifyDetailsById", method = RequestMethod.GET)
    @ApiOperation("根据id查看二级分类详细信息")
    @IgnoreSecurity
    @ResponseBody
    public Result selectClassifyDetailsById(Long id) {
        return ResultUtil.success(helpClassifyDetailsService.selectById(id));
    }


    @RequestMapping(value = "/selectClassifyDetailsListByClassifyId", method = RequestMethod.GET)
    @ApiOperation("根据一级分类id查看二级分类")
    @IgnoreSecurity
    @ResponseBody
    public Result selectClassifyDetailsListByClassifyId(Long id) {
        return ResultUtil.success(helpClassifyDetailsService.selectList(id));
    }

    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ApiOperation("查看一二级分类列表")
    @IgnoreSecurity
    @ResponseBody
    public Result select() {
        return ResultUtil.success(helpClassifyService.getHelpClassifyModel());
    }

    @RequestMapping(value = "/selectHelpTaskByClassify", method = RequestMethod.GET)
    @ApiOperation("根据分类查找任务")
    @IgnoreSecurity
    @ResponseBody
    public Result selectHelpTaskByClassify(Long classifyDetailsId,int size,int page) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(helpClassifyService.selectHelpTaskByClassify(classifyDetailsId,pageable));
    }

    @RequestMapping(value = "/selectHelpTaskByTitle", method = RequestMethod.GET)
    @ApiOperation("根据名称模糊查找任务")
    @IgnoreSecurity
    @ResponseBody
    public Result selectHelpTaskByTitle(String title,int size,int page) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(helpClassifyService.selectByTitle(title,pageable));
    }

}